<?php

namespace Kunfeng\Dify\Client;

use GuzzleHttp\Client;
use Kunfeng\Dify\Model\DifyConfig;
use Kunfeng\Dify\Client\Impl\DifyClient;
use Kunfeng\Dify\Client\Impl\DifyChatClient;
use Kunfeng\Dify\Client\Impl\DifyCompletionClient;
use Kunfeng\Dify\Client\Impl\DifyWorkflowClient;
use Kunfeng\Dify\Client\Impl\DifyDatasetsClient;
use Kunfeng\Dify\Client\Impl\DifyChatflowClient;
use Kunfeng\Dify\Client\RetryHandler;

/**
 * Dify 客户端工厂类
 */
class DifyClientFactory
{
    /**
     * 创建完整的 Dify 客户端
     *
     * @param string $baseUrl 基础URL
     * @param string $apiKey API密钥
     * @param DifyConfig|null $config 配置
     * @return DifyClientInterface
     */
    public static function createClient(
        string $baseUrl, 
        string $apiKey, 
        ?DifyConfig $config = null
    ): DifyClientInterface {
        $httpClient = ($config && $config->getHttpOptions()) ? new Client($config->getHttpOptions()) : null;
        $retryHandler = $config ? \Kunfeng\Dify\Client\RetryHandler::createDefault() : null;
        return new \Kunfeng\Dify\Client\Impl\DifyClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 创建对话型应用客户端
     *
     * @param string $baseUrl 基础URL
     * @param string $apiKey API密钥
     * @param DifyConfig|null $config 配置
     * @return DifyChatClientInterface
     */
    public static function createChatClient(
        string $baseUrl, 
        string $apiKey, 
        ?DifyConfig $config = null
    ): DifyChatClientInterface {
        $httpClient = ($config && $config->getHttpOptions()) ? new Client($config->getHttpOptions()) : null;
        $retryHandler = $config ? \Kunfeng\Dify\Client\RetryHandler::createDefault() : null;
        return new \Kunfeng\Dify\Client\Impl\DifyChatClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 创建文本生成型应用客户端
     *
     * @param string $baseUrl 基础URL
     * @param string $apiKey API密钥
     * @param DifyConfig|null $config 配置
     * @return DifyCompletionClientInterface
     */
    public static function createCompletionClient(
        string $baseUrl, 
        string $apiKey, 
        ?DifyConfig $config = null
    ): DifyCompletionClientInterface {
        $httpClient = ($config && $config->getHttpOptions()) ? new Client($config->getHttpOptions()) : null;
        $retryHandler = $config ? \Kunfeng\Dify\Client\RetryHandler::createDefault() : null;
        return new \Kunfeng\Dify\Client\Impl\DifyCompletionClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 创建工作流应用客户端
     *
     * @param string $baseUrl 基础URL
     * @param string $apiKey API密钥
     * @param DifyConfig|null $config 配置
     * @return DifyWorkflowClientInterface
     */
    public static function createWorkflowClient(
        string $baseUrl, 
        string $apiKey, 
        ?DifyConfig $config = null
    ): DifyWorkflowClientInterface {
        $httpClient = ($config && $config->getHttpOptions()) ? new Client($config->getHttpOptions()) : null;
        $retryHandler = $config ? \Kunfeng\Dify\Client\RetryHandler::createDefault() : null;
        return new \Kunfeng\Dify\Client\Impl\DifyWorkflowClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 创建知识库管理客户端
     *
     * @param string $baseUrl 基础URL
     * @param string $apiKey API密钥
     * @param DifyConfig|null $config 配置
     * @return DifyDatasetsClientInterface
     */
    public static function createDatasetsClient(
        string $baseUrl, 
        string $apiKey, 
        ?DifyConfig $config = null
    ): DifyDatasetsClientInterface {
        $httpClient = ($config && $config->getHttpOptions()) ? new Client($config->getHttpOptions()) : null;
        $retryHandler = $config ? \Kunfeng\Dify\Client\RetryHandler::createDefault() : null;
        return new \Kunfeng\Dify\Client\Impl\DifyDatasetsClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 创建工作流编排对话型应用客户端
     *
     * @param string $baseUrl 基础URL
     * @param string $apiKey API密钥
     * @param DifyConfig|null $config 配置
     * @return DifyChatflowClientInterface
     */
    public static function createChatflowClient(
        string $baseUrl, 
        string $apiKey, 
        ?DifyConfig $config = null
    ): DifyChatflowClientInterface {
        $httpClient = ($config && $config->getHttpOptions()) ? new Client($config->getHttpOptions()) : null;
        $retryHandler = $config ? RetryHandler::createDefault() : null;
        return new DifyChatflowClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 使用配置创建客户端
     *
     * @param DifyConfig $config 配置
     * @return DifyClientInterface
     */
    public static function createClientFromConfig(DifyConfig $config): DifyClientInterface
    {
        return self::createClient($config->getBaseUrl(), $config->getApiKey(), $config);
    }
}
